# 导入gradio库
import gradio as gr
import requests

# 编写函数
def mockdeepseek(question):
    print(question)
    return f"接收到问题：{question}，大模型回复：我是专属人工智能（开发中）"

# TODO 写真的deepseek函数
def deepseek(question):
    url = "https://api.siliconflow.cn/v1/chat/completions"

    payload = {
        "model": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
        "stream": False,
        "max_tokens": 1024,
        "temperature": 0.7,
        "top_p": 0.7,
        "top_k": 50,
        "frequency_penalty": 0.5,
        "n": 1,
        "stop": [],
        "messages": [
            {
                "role": "user",
                "content": question
            }
        ]
    }
    headers = {
        "Authorization": "Bearer sk-ztjdijjdzgjnmilnsbwtogdvadchciehkjgfuspwhirvvvdw",
        "Content-Type": "application/json"
    }

    response = requests.request("POST", url, json=payload, headers=headers)

    # print(response.text)
    # 可以使用JSON提取回答信息
    return response.json().get("choices")[0].get("message").get("content")
    # TODO 优化返回值


# 构建交互界面
demo = gr.Interface(
    fn=deepseek,
    inputs=["text"],
    outputs=["text"]
)

# 启动界面
demo.launch(server_name="0.0.0.0")
# 0.0.0.0 为广播地址， 127.0.0.1 为本地回环地址


